home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 099 (1989-05-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 099 (1989-05-15)(Ossowski, Stefan)(DE)(PD).adf / PCQ / Runtime / openers.asm < prev    next >
Assembly Source File  |  1989-03-31  |  4KB  |  149 lines

  1.  
  2. *    Openers.asm (of PCQ Pascal runtime library)
  3. *    Copyright (c) 1989 Patrick Quaid
  4.  
  5. *    This file takes care of opening and closing DOS files.  In
  6. *    much the same way as the memory routines, these routines keep a
  7. *    list of the open files around.  Open() puts the files on the list
  8. *    and Close() takes them off.
  9.  
  10.     SECTION    ONE
  11.  
  12.     XREF    _p%DOSBase
  13.     XREF    _LVOOpen
  14.     XREF    _p%new
  15.     XREF    _p%dispose
  16.     XREF    _p%wrapitup
  17.     XREF    _LVORead
  18.     XREF    _LVOClose
  19.  
  20.     XDEF    filekey
  21.  
  22. *    algorithm for open:
  23. *
  24. *    open the file
  25. *    set the link
  26. *    store the file handle
  27. *    if the record size is not 1, 2, or 4
  28. *        call _p%new to allocate a buffer
  29. *        store the address of the buffer
  30. *    if it's an input file
  31. *        read the first record
  32. *        set the eof field accordingly
  33.  
  34.     XDEF    _p%open
  35. _p%open:
  36.  
  37.     move.l    d2,-(sp)    ; save the access mode
  38.     move.l    a0,-(sp)    ; save address of file record
  39.     move.l    d0,d1        ; get address of file name string
  40.     move.l    _p%DOSBase,a6    ; d2 already has mode
  41.     jsr    _LVOOpen(a6)
  42.     tst.l    d0        ; opened ok?
  43.     bne.s    1$        ; if so, skip this
  44.     add.l    #8,sp        ; restore stack
  45.     rts            ; and return
  46.  
  47. 1$    move.l    (sp)+,a0    ; get address back
  48.     move.l    d0,(a0)        ; save file handle in record
  49.     move.l    filekey,14(a0)    ; save link
  50.     move.l    a0,filekey    ; add to list
  51.     move.l    8(a0),d0    ; get size
  52.     cmp.l    #1,d0        ; is it 1?
  53.     beq.s    2$        ; if so, skip this bit here.
  54.     cmp.l    #2,d0        ; ditto if two or four
  55.     beq.s    2$
  56.     cmp.l    #4,d0
  57.     beq.s    2$
  58.     move.l    a0,-(sp)    ; save address (again)
  59.     jsr    _p%new        ; get block of size in d0, left in d0
  60.     move.l    (sp)+,a0    ; get address back again
  61.     move.l    d0,4(a0)    ; store the pointer to the new block
  62.     bne.s    2$        ; if it's ok, go on.
  63.     jmp    _p%wrapitup    ; no memory for file.  Abort!
  64. 2$    move.b    #0,12(a0)    ; set eof = false
  65.     move.l    (sp)+,d0    ; retrieve access mode
  66.     cmp.l    #1005,d0    ; is it 1005?
  67.     bne.s    3$        ; if so, then it's an input file.
  68.  
  69. * at this point, a0 has the address of the file block, which
  70. * is set up correctly except for the data direction.  Thus we'll
  71. * set that, then just call the arbitrary read routine on the file.
  72.  
  73.     move.b    #0,13(a0)    ; set inout = input
  74.     jsr    _p%readarbbuf    ; read an arbitrary datum into buffer
  75.                 ; that routine sets the eof flag correctly
  76. 3$    move.l    #-1,d0        ; regardless of how the read went, the
  77.     rts            ; file IS open, so return true.
  78.  
  79.     XDEF    _p%readarbbuf
  80. _p%readarbbuf
  81.  
  82. * read 'record length' bytes from 'filehandle' into the record's buffer
  83. * a0 is the file record
  84. * set the eof field accordingly
  85.  
  86.     move.l    a0,-(sp)
  87.     move.l    (a0),d1        ; get file handle
  88.     move.l    8(a0),d3    ; get length
  89.     lea    4(a0),a1
  90.     move.l    a1,d2        ; get address of buffer.
  91.                 ; this is correct for size = 1,2 or 3.
  92.                 ; figure most will be that size, right?
  93.     cmp.l    #1,d3        ; is it, in fact, 1?
  94.     beq.s    1$        ; if so, then skip correction
  95.     cmp.l    #2,d3        ; same with 2
  96.     beq.s    1$
  97.     cmp.l    #4,d3        ; and 4
  98.     beq.s    1$
  99.     move.l    4(a0),d2    ; load ADDRESS of buffer (correct the assumption)
  100. 1$    move.l    _p%DOSBase,a6
  101.     jsr    _LVORead(a6)
  102.     move.l    (sp)+,a0    ; retrieve address
  103.     cmp.l    8(a0),d0    ; did everything go ok?
  104.     beq.s    2$        ; yes, so just leave
  105.     move.b    #-1,12(a0)    ; eof = true
  106. 2$    rts
  107.  
  108.     XDEF    _p%close
  109. _p%close:
  110.  
  111.     move.l    d0,a0        ; get address into a0
  112.  
  113. 1$    cmp.l    filekey,a0    ; is this first open file?
  114.     bne.s    2$        ; if not, go around
  115.     move.l    14(a0),filekey    ; bypass record
  116.     bra    5$        ; and close file
  117. 2$    move.l    filekey,a1
  118.     move.l    14(a1),a2    ; a1 is trailer, a2 is node
  119. 3$    cmp.l    a2,a0        ; is this node the original?
  120.     beq.s    4$        ; if so, go
  121.     move.l    a2,a1
  122.     move.l    14(a2),a2    ; set up for next
  123.     move.l    a2,d0        ; is this zero?
  124.     bne    3$
  125.     rts            ; wasn't in list! Abort!
  126. 4$    move.l    14(a2),a2    ; get next
  127.     move.l    a2,14(a1)    ; bypass this node
  128.  
  129. 5$    move.l    (a0),d1
  130.     move.l    a0,-(sp)
  131.     move.l    _p%DOSBase,a6
  132.     jsr    _LVOClose(a6)
  133.     move.l    (sp)+,a0
  134.     move.l    8(a0),d0
  135.     cmp.l    #1,d0        ; if size = 1 then no buffer
  136.     beq.s    6$        ; so skip
  137.     cmp.l    #2,d0        ; same for 2
  138.     beq.s    6$
  139.     cmp.l    #4,d0        ; and 4
  140.     beq.s    6$
  141.     move.l    4(a0),d0    ; get address of block
  142.     jsr    _p%dispose    ; free the buffer
  143. 6$    rts
  144.  
  145.     SECTION    TWO
  146. filekey    dc.l    0
  147.     END
  148.  
  149.